/**
 * axios请求/响应封装
 */
import axios from "axios";
// import * as qs from 'qs'

// 请求超时时间
axios.defaults.timeout = 50000;
// axios.defaults.withCredentials = true
axios.defaults.headers.delete["Content-Type"] =
  "application/json;charset=UTF-8";
/* eslint-disable no-undef */
if (process.env.NODE_ENV === "development") {
  axios.defaults.baseURL = defaultConfig.baseDevUrl;
} else if (process.env.NODE_ENV === "testing") {
  axios.defaults.baseURL = defaultConfig.baseDevUrl;
} else if (process.env.NODE_ENV === "production") {
  axios.defaults.baseURL = defaultConfig.baseProdUrl;
}

// request拦截器
axios.interceptors.request.use(
  config => {
    // do something before request is sent
    // 这里可以给每个请求添加Token
    // config.transformRequest = [(data, headers) => {
    //     return qs.stringify(data, {
    //         allowDots: true
    //     });
    // }];
    // config.paramsSerializer = (params) => {
    //     return qs.stringify(params, {
    //         arrayFormat: 'repeat'
    //     });
    // };
    return config;
  },
  error => {
    // do something with request error
    console.log(error);
    return Promise.reject(error);
  }
);

// response 拦截器
/**
 * 通过response自定义code来标示请求状态，当code返回如下情况为权限有问题，登出并返回到登录页
 * 如通过xmlhttprequest 状态码标识 逻辑可写在下面error中
 */
axios.interceptors.response.use(
  response => {
    const res = response.data;
    if (res.retCode === "401") {
      // 登陆过期
    } else {
      return Promise.resolve(response);
    }
  },
  error => {
    return Promise.reject(error.response);
  }
);

/**
 * get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function get(url, params = {}) {
  return new Promise((resolve, reject) => {
    axios
      .get(url, {
        params
      })
      .then(response => {
        resolve(response);
      })
      .catch(err => {
        reject(err);
      });
  });
}
/**
 * post方法，对应post请求
 * @param {String} url [请求的url地址]
 * @param {Object} data [请求时携带的参数]
 */
export function post(url, data = {}) {
  // 是否可以发起请求 目的是检测是否未登录
  // Todos: axios 拦截response 对状态码进行判断 统一拦截
  let canPass = true;
  // let { pathname } = location;
  // pathname = pathname.toLowerCase();
  // if (pathname === '/login' || pathname === '/regist' || pathname === '/autologin') {
  //     canPass = true;
  // } else if (!localStorage.getExpire('token')) {
  //     canPass = false;
  // }
  if (canPass) {
    return new Promise((resolve, reject) => {
      axios
        .post(url, data)
        .then(response => {
          resolve(response);
        })
        .catch(err => {
          reject(err);
        });
    });
  } else {
    // window.location.hash = '#/login';
    window.location.href = "/login";
  }
}

/**
 * put方法，对应put请求
 * @param {String} url [请求的url地址]
 * @param {Object} data [请求时携带的参数]
 */
export function put(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios
      .put(url, data)
      .then(response => {
        resolve(response);
      })
      .catch(err => {
        reject(err);
      });
  });
}
/**
 * delete方法，对应delete请求
 * @param {String} url [请求的url地址]
 * @param {Object} data [请求时携带的参数]
 */

export function Delete(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios
      .delete(url, data)
      .then(response => {
        resolve(response);
      })
      .catch(err => {
        reject(err);
      });
  });
}

export const baseUrl = axios.defaults.baseURL;

export default axios;
